#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Repo Changelog (Base code from AICP repos 2015) ===
function repochangelog()
{
  # Usage: repochangelog <days_count> [project1_path,project2_path,...] (Generate ROM changelogs)

  # Exports
  export red;
  export grn;
  export ylw;
  export blu;
  export ppl;
  export cya;
  export txtbld;
  export bldred;
  export bldgrn;
  export bldylw;
  export bldblu;
  export bldppl;
  export bldcya;
  export txtrst;
  export rev;
  export pplrev;
  export cyarev;
  export ylwrev;
  export blurev;
  export normal;
  export After_Date;
  export Until_Date;

  # Load colors
  red=$(tput setaf 1);             #  red
  grn=$(tput setaf 2);             #  green
  ylw=$(tput setaf 3);             #  yellow
  blu=$(tput setaf 4);             #  blue
  ppl=$(tput setaf 5);             #  purple
  cya=$(tput setaf 6);             #  cyan
  txtbld=$(tput bold);             #  Bold
  bldred=${txtbld}$(tput setaf 1); #  red
  bldgrn=${txtbld}$(tput setaf 2); #  green
  bldylw=${txtbld}$(tput setaf 3); #  yellow
  bldblu=${txtbld}$(tput setaf 4); #  blue
  bldppl=${txtbld}$(tput setaf 5); #  purple
  bldcya=${txtbld}$(tput setaf 6); #  cyan
  txtrst=$(tput sgr0);             #  Reset
  rev=$(tput rev);                 #  Reverse color
  pplrev=${rev}$(tput setaf 5);
  cyarev=${rev}$(tput setaf 6);
  ylwrev=${rev}$(tput setaf 3);
  blurev=${rev}$(tput setaf 4);
  normal='tput sgr0';

  # Repo detection
  if [ -z "${ANDROID_BUILD_TOP}" ] || [ -z "${OUT}" ]; then
    echo ' repochangelog can only be used in a build ready environment';
    echo '';
    return;
  fi;

  # Variables
  local cwd;
  local changelogname=Changelog.txt;
  local changelog=${OUT}/${changelogname};

  # Repo root
  cwd=$(pwd);
  cd "${ANDROID_BUILD_TOP}";

  local changelogdays=5;
  if [ ! -z "${1}" ]; then
    changelogdays=${1};
  fi;

  # Prepare and clean the log
  mkdir -p "${OUT}/";
  if [ -f "${changelog}" ]; then
    rm -f "${changelog}";
  fi;
  touch "${changelog}";

  # Print something to build output
  echo '';
  echo "${bldppl} Generating changelog...${txtrst}";

  for i in $(seq "${changelogdays}"); do
    After_Date=$(date --date="${i} days ago" +%m-%d-%Y);
    Until_Date=$(date --date="$((i - 1)) days ago" +%m-%d-%Y);

    # Line with after --- until was too long for a small ListView
    echo "${ylw}  Processing ${Until_Date}...${txtrst}";
    { echo ' ======================';
      echo "  ChangeLog ${Until_Date}";
      echo ' ======================'; } >> "${changelog}";

    # Handle the usage of repochangelog days_count project1_path,project2_path,...
    if [ ! -z "${2}" ]; then
      # Cycle through the selected projects to find commits between 2 dates
      for projectpath in ${2//,/ }; do
        if [ -d "${projectpath}" ]; then
          cd "${projectpath}/";
          { echo '';
            echo "project ${projectpath}/";
            git log --oneline --pretty="tformat:    %h - %s (%cr) <%an>" --after="${After_Date}" --until="${Until_Date}"; } >> "${changelog}";
          cd "${ANDROID_BUILD_TOP}";
        fi;
      done;
    else
      # Cycle through every repo to find commits between 2 dates
      repo forall -pc "git log --oneline --pretty=\"tformat:    %h - %s (%cr) <%an>\" --after=${After_Date} --until=${Until_Date}" >> "${changelog}";
    fi;
    echo >> "${changelog}";
  done;

  sed -i 's/project \(.*\)\// * \1\n/g' "${changelog}";

  # Copy change to system/etc/
  mkdir -p "${OUT}/system/etc/";
  if [ -e "${OUT}/system/etc/${changelogname}" ]; then
    rm "${OUT}/system/etc/${changelogname}";
  fi;
  cp "${changelog}" "${OUT}/system/etc/";
  cd "${cwd}";

  echo "${bldcya} ChangeLog saved:${txtrst} ${txtbld}${changelog}${txtrst}";
  echo '';
}
